using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.IO;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class AdList : AdminBaseUserControl
    {
        public event EventHandler EditAd;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindAd(object sender, EventArgs e)
        {
            ClassList ucClassList = (ClassList)sender;
            BindAd(Convert.ToInt64(ucClassList.SelectedValue));
        }

        public void BindAd(long classID)
        {
            lblStatus.Text = "Advertise Lists";
            ICmsClass clazz = ClassController.GetClass(classID);
            Session[SessionVariable.Clazz.CurrentID] = classID;
            gvAd.DataSource = AdController.GetAdList(classID);
            gvAd.DataBind();
            ViewState["AdID"] = null;
        }

        public void DoDelete(object sender, EventArgs e)
        {
            if (ViewState["AdID"] != null) AdController.DeleteAd(Convert.ToInt64(ViewState["UserID"]));
            IAdContent ad = AdController.GetAd(Convert.ToInt64(ViewState["UserID"]));
            string path = Path.Combine(DataRootPhysicalPath, "Ads/" + ad.ID + ad.Extention);
            FileInfo file = new FileInfo(path);
            file.Delete();

            BindAd(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            BindAd(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));

        }
        public string SelectedID
        {
            get { return Convert.ToString(ViewState["AdID"]); }
            set { ViewState["AdID"] = value; }
        }

        protected void gvAd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            long ID = Convert.ToInt64(e.CommandArgument);
            if (e.CommandName == "editAd")
            {

                ViewState["AdID"] = ID;
                if (EditAd != null)
                {
                    EditAd(this, new EventArgs());
                }
            }

            if (e.CommandName == "delAd")
            {
                AdController.DeleteAd(ID);
                BindAd(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            }
        }
    }
}